package zabbix

import (
	"encoding/json"
	"errors"
)

func ImageCreateData() (string,map[string]interface{}) {
	method := "image.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "imagetype": 1,
        "name": "Cloud_(24)",
        "image": "9XfPLjTCW84r+cn7JcRK5CYII="
    },
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ImageDeleteData() (string,map[string]interface{}) {
	method := "image.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
		"188",
		"192"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ImageUpdateData() (string,map[string]interface{}) {
	method := "image.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "imageid": "2",
        "name": "Cloud icon"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ImageGetData() (string,map[string]interface{}) {
	method := "image.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "select_image": true,
        "imageids": "2"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func ImageCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ImageCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ImageDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ImageDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ImageUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ImageUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ImageGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ImageGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



